/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: 86136
 * Date: 2025-04-13
 * Time: 20:12
 */
public class Demo28 {
    public static void main(String[] args) throws InterruptedException {
        Object locker1 = new Object();
        Object locker2 = new Object();

        //两个线程，两把锁，尝试获取对方的锁 -- 死锁
        Thread t1 = new Thread(() -> {
            //死锁
            synchronized (locker1) {
                //拿醋
                try {
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                //尝试拿酱油
                synchronized (locker2) {
                    System.out.println("t1两个锁都拿到了");
                }
            }
        });
        Thread t2= new Thread(() -> {
            //死锁
            synchronized (locker2) {
                //拿酱油
                try {
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                //尝试拿醋
                synchronized (locker1) {
                    System.out.println("t2两个锁都拿到了");
                }
            }
        });
        t1.start();
        t2.start();
        t1.join();
        t2.join();
    }
}
